package com.power.ding.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.ding.dao.Di_rel_dept_targetDao;
import com.power.ding.model.Di_rel_dept_target;
import com.power.ding.model.pageInfo.Di_rel_dept_targetPageInfo;
import com.power.ding.model.pojo.vo.Di_rel_dept_targetVO;
import com.power.ding.service.Di_rel_dept_targetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *  司法组织机构-被申请人业务层实现
 */
@Service
public class Di_rel_dept_targetServiceImpl extends BaseServiceImpl implements Di_rel_dept_targetService {

    @Autowired
    private Di_rel_dept_targetDao dao;

    @Override
    public Di_rel_dept_target getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Di_rel_dept_target di_rel_dept_target) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (di_rel_dept_target.getPrimaryKey() != null) {
        	 Di_rel_dept_target di_rel_dept_targetfDb = dao.findById(di_rel_dept_target.getPrimaryKey()).orElse(null);
             if(di_rel_dept_targetfDb != null) {
                BeanUtils.copyProperties(di_rel_dept_target, di_rel_dept_targetfDb);
                dao.save(di_rel_dept_targetfDb);
                return;
             }
        }
        dao.save(di_rel_dept_target);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Di_rel_dept_target> findAll(Di_rel_dept_targetPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Di_rel_dept_target> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Di_rel_dept_target> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Di_rel_dept_targetVO> queryAllTargetOrg() {
        List<Map<String,Object>> targetOrgList = this.dao.queryAllTargetOrg();
        if (CollectionUtil.isNotEmpty(targetOrgList)) {
            return JSONArray.parseArray(JSON.toJSONString(targetOrgList), Di_rel_dept_targetVO.class);
        }
        return Collections.emptyList();
    }
}
